CONTROL STATEMENTS



What are Control statements?

Control statements control the flow of the program. They enable the computer to know that whether to execute certain set of statements or not under a given condition


If

It is used to specify a block of code to be executed if specified condition is true.

Else

It is used to specify a block of code to be executed if the condition specified in ‘if’ is false.

Switch

This statement is used when we need to execute a single set of statements under specified value from a given list of values. In other words it executes one block of statement among many alternatives.

Syntax Input Output
#include<stdio.h>
main()
{
switch (choice)
{	
case 1:
_____;
_____;
case 2:
_____;
_____;
case 3:
_____;
_____;
}
getch();
}
#include<stdio.h>
main()
{
int a;
clrscr();
a=4;
switch (a)
{
case 1:
printf("Monday");
case 2:
printf("\nTuesday");
case 3:
printf("\nWednesday");
case 4:
printf("\nThursday");
case 5:
printf("\nFriday");
case 6:
printf("\nSaturday");
case 7:
printf("\nSunday");
}
getch();
}



For

This statement allows you to repeat a block of statements a specific number of times according to the given condition.


Syntax Input Output
#include<stdio.h>
main()
{
int a;
for(initializing part, conditional part, inc/dec)
{	
________;
________;
________;
}
getch();
}
#include<stdio.h>
main()
{
int i;
for(i=1;i<=30;i++)
{
printf("\n%d",i);
}
getch();
}




While

This statement allows you to repeat a block of statements a specific number of times according to the given condition. Here the initializing part is mentioned before ‘while’ where the variable is assigned value and the conditional part where the condition is specified is mentioned with while and lastly the increment/ decrement part is mentioned under while.


Syntax Input Output
#include<stdio.h>
main()
{
int a;
a=value;
while(condition)
{	
________;
________;
Ind/dec;
}
getch();
}
#include<stdio.h>
main()
{
int i;
i=0;
while (i<=50)
{
printf ("%d\n",i);
i=i+5;
}
getch();
}




Do While

This statement allows you to repeat a block of statements a specific number of times according to the given condition. Here the initializing part is mentioned before ‘while’ where the variable is assigned value and the increment/ decrement part is mentioned under do. and lastly the conditional part where the condition is specified is mentioned under while


Syntax Input Output
#include<stdio.h>
main()
{
int i;
i=value;
do
{
________;
________;
inc/dec;
}
while (conditional part);
getch();
}
#include<stdio.h>
main()
{
int i;
clrscr();
i=0;
do
{
printf ("%d\n",i);
i=i+9;
}
while(i<100);
getch();
}